<template>
  <div class="news-detail-container">
    <el-card>
      <div class="news-header">
        <h1>{{ news.title }}</h1>
        <div class="news-meta">
          <el-tag size="small">{{ news.category }}</el-tag>
          <span class="publish-time">发布时间：{{ formatDate(news.createdAt) }}</span>
        </div>
      </div>

      <div class="news-content" v-html="news.content"></div>

      <div class="actions">
        <el-button @click="$router.push('/news')">返回列表</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

export default {
  name: 'NewsDetail',
  setup() {
    const route = useRoute()
    const store = useStore()
    const news = ref({})

    const loadNewsDetail = async () => {
      try {
        const response = await store.dispatch('fetchNewsDetail', route.params.id)
        news.value = response.data
      } catch (error) {
        ElMessage.error('获取新闻详情失败')
      }
    }

    const formatDate = (date) => {
      return dayjs(date).format('YYYY-MM-DD HH:mm')
    }

    onMounted(() => {
      loadNewsDetail()
    })

    return {
      news,
      formatDate
    }
  }
}
</script>

<style scoped>
.news-detail-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.news-header {
  margin-bottom: 30px;
}

h1 {
  margin: 0 0 15px 0;
  font-size: 24px;
  color: #303133;
}

.news-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  color: #909399;
}

.publish-time {
  font-size: 14px;
}

.news-content {
  line-height: 1.8;
  color: #303133;
  margin: 20px 0;
}

.actions {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}
</style>
